﻿using client;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilLib;
using UtilPublic.Serialization;

namespace ConfClient
{
    public static partial class Conf
    {
        public static string ConfPathJson = $"./../config/json/";

        public static bool InitConf()
        {
            bool result = true;

            result = InitConfSettings();

            if (result)
            {
                App.Debug("读取策划配置文件成功");
            }
            else
            {
                App.Error("读取策划配置文件失败");
                return result;
            }

            return result;
        }

        public static bool ReadConfig<T1, T2>(string Name, ref Dictionary<T1, T2> confDic)
        {
            return ReadJsonDict(Name, ref confDic, ConfPathJson);
        }
        public static bool ReadJsonDict<T1, T2>(string Name, ref Dictionary<T1, T2> confDic, string pathName)
        {
            string ConfName = $"{pathName}{Name}.json";
            try
            {
                FileStream fs_PTL = new FileStream(ConfName, FileMode.Open);
                StreamReader sr_PTL = new StreamReader(fs_PTL, Encoding.UTF8);
                try
                {
                    var text = sr_PTL.ReadToEnd();
                    if (text.IsNullOrEmpty())
                    {
                        text = "";
                    }

                    confDic = Jsh.Jobj<Dictionary<T1, T2>>(text);

                    App.Debug($"读取配置-> {ConfName,-40} <-成功 共{confDic.Count}项");
                }
                catch
                {
                    //App.Error($"读取配置-> {ConfName,-40} <-失败", e);
                    App.Error($"读取配置-> {ConfName,-40} <-失败");
                    return false;
                }
                finally
                {
                    sr_PTL.Close();
                }

            }
            catch (Exception)
            {
                App.Error($"读取配置-> {ConfName,-40} <-失败2");
                return false;
            }


            return true;
        }
    }
    public interface IConfigBase
    {
    }
}
